if (condition) continue; OR if (!condition) { ... }? (style preference)
Posted
by Hosam Aly
on Stack Overflow
See other posts from Stack Overflow
or by Hosam Aly
Published on 2008-12-24T14:18:47Z
Indexed on
2010/04/04
5:43 UTC
Read the original article
Hit count: 484
subjective
|coding-style
I know this is a matter of style, hence the subjective tag. I have a small piece of code, with two nested conditions. I could code it in two ways, and I'd like to see how more experienced developers think it should look like.
Style 1:
while (!String.IsNullOrEmpty(msg = reader.readMsg()))
{
RaiseMessageReceived();
if (parseMsg)
{
ParsedMsg parsedMsg = parser.parseMsg(msg);
RaiseMessageParsed();
if (processMsg)
{
process(parsedMsg);
RaiseMessageProcessed();
}
}
}
Style 2:
while (!String.IsNullOrEmpty(msg = reader.readMsg()))
{
RaiseMessageReceived();
if (!parseMsg) continue;
ParsedMsg parsedMsg = parser.parseMsg(msg);
RaiseMessageParsed();
if (!processMsg) continue;
process(parsedMsg);
RaiseMessageProcessed();
}
(Side question: how do I put empty lines in the source code sample?)
© Stack Overflow or respective owner